﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Agile_E
{
    public partial class getCustomer : System.Web.UI.Page
    {
        private static Customer getCust;
        private static int cust_id;

        protected void Page_Load(object sender, EventArgs e)
        {   
            if(!User.IsInRole("Manager"))
            {
                btnEdit.Visible = false;
                btnEdit.Enabled = false;
                btnSave.Visible = false;
                btnSave.Enabled = false;
            }

            if (!IsPostBack)
            {
                try
                {
                    cust_id = Convert.ToInt32(Request.QueryString["Customer_number"]);
                    DatabaseHandler.connect();
                    getCust = DatabaseHandler.getCustomer(cust_id);
                    txtlname.Text = getCust.Info.LastName;
                    txtmname.Text = getCust.Info.MiddleName;
                    txtfname.Text = getCust.Info.FirstName;
                    txtcphone.Text = getCust.Info.Cell;
                    txthphone.Text = getCust.Info.Homephone;
                    txtWphone.Text = getCust.Info.Workphone;
                    txtstreet.Text = getCust.Info.Address;
                    txtstate.Text = getCust.Info.State;
                    txtcity.Text = getCust.Info.City;
                    txtzip.Text = getCust.Info.Zip;
                    txtInterest.Text = getCust.Interested;
                    txtnotes.Text = getCust.AdditionalInfo;
                    lblcust.Text = getCust.Info.FirstName + " " + getCust.Info.MiddleName + " " + getCust.Info.LastName;
                    
                }
                catch (Exception ex)
                {
                    ((Main)((SearchMP)this.Master).Master).writeStatus(ex.Message, true);
                }
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                btnEdit.Visible = false;
                lbltitle.Text = "Update Customer: ";
                lblcust.Text = string.Empty;
                btnSave.Visible = true;
                changeTheme(true);
                blockWrite(false);
            }
            catch (Exception ex)
            {
                ((Main)((SearchMP)this.Master).Master).writeStatus(ex.Message, true);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Customer updateCust = new Customer();
                updateCust.Customer_ID = cust_id;
                updateCust.Info.LastName = txtlname.Text;
                updateCust.Info.MiddleName = txtmname.Text;
                updateCust.Info.FirstName = txtfname.Text;
                updateCust.Info.Cell = txtcphone.Text;
                updateCust.Info.Homephone = txthphone.Text;
                updateCust.Info.Workphone = txtWphone.Text;
                updateCust.Info.Address = txtstreet.Text;
                updateCust.Info.State = txtstate.Text;
                updateCust.Info.City = txtcity.Text;
                updateCust.Info.Zip = txtzip.Text;
                updateCust.Interested = txtInterest.Text;
                updateCust.AdditionalInfo = txtnotes.Text;

                DatabaseHandler.connect();
                DatabaseHandler.updateCustomer(updateCust);
                btnSave.Visible = false;
                btnEdit.Visible = true;
                changeTheme(false);
                blockWrite(true);
                lbltitle.Text = "Customer: ";
                lblcust.Text = updateCust.Info.FirstName + " " + updateCust.Info.MiddleName + " " + updateCust.Info.LastName;
                ((Main)((SearchMP)this.Master).Master).writeStatus("Changes saved", false);
            }
            catch (Exception ex)
            {
                ((Main)((SearchMP)this.Master).Master).writeStatus(ex.Message, true);
            }
        }

        private void blockWrite(bool read)
        {
            txtfname.ReadOnly = read;
            txtmname.ReadOnly = read;
            txtlname.ReadOnly = read;
            txtcphone.ReadOnly = read;
            txthphone.ReadOnly = read;
            txtWphone.ReadOnly = read;
            txtstreet.ReadOnly = read;
            txtcity.ReadOnly = read;
            txtstate.ReadOnly = read;
            txtzip.ReadOnly = read;
            txtInterest.ReadOnly = read;
            txtnotes.ReadOnly = read;
        }

        private void changeTheme(bool update)
        {
            string cssclass;
            if (update)
                cssclass = "updateDealershiptxtbox";
            else
                cssclass = "Dealershiptxtbox";

            txtfname.CssClass = cssclass;
            txtmname.CssClass = cssclass;
            txtlname.CssClass = cssclass;
            txtcphone.CssClass = cssclass;
            txthphone.CssClass = cssclass;
            txtWphone.CssClass = cssclass;
            txtstreet.CssClass = cssclass;
            txtcity.CssClass = cssclass;
            txtstate.CssClass = cssclass;
            txtzip.CssClass = cssclass;
            txtInterest.CssClass = cssclass;
            txtnotes.CssClass = cssclass;
        }
    }
}
